package cs2340.droidceptiontodo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
 * Activity class takes in a username and password from the login
 * screen and logs the user in to their account
 * 
 */
public class LoginActivity extends Activity implements OnClickListener {
	EditText loginName;
	EditText passwordName;
	Button login;
	Button register;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        loginName = (EditText)findViewById(R.id.editText1);
        passwordName = (EditText)findViewById(R.id.editText2);
        login = (Button)findViewById(R.id.button1);
        register = (Button)findViewById(R.id.button2);
        login.setOnClickListener(this);
        register.setOnClickListener(this);
    }

    @Override
    /**
     * Validates the information entered by the user and logs the
     * user in if the username and password are accurate. 
     * Otherwise, displays an error message.
     * 
     */
	public void onClick(View v) {
		// password will be assigned with the current value entered in edittext1
		String password = null;
		//passwordFound will be assigned what is found in the file
		String passwordFound = null;
		//message to be displayed depending on result
		String logMessage = "";
		//if LogIn is clicked
		if(v.getId() == (login.getId())) { 
			//retrieves username and password from fields and assigns them to variables
			String lgn = loginName.getText().toString();
			password = passwordName.getText().toString();
			
			//creates a database object, which in reality just pulls up the old database
			//Database checker = new Database(this);
			Database checker = Database.getInstance(this);
			try{
				//opens the database
				checker.open();
				//stores the int result of the log in checker
				int res = checker.checkUser(lgn, password);
				//closes the database
				checker.close();
				if(res == -1){
					logMessage = ("User was not found. Click register button to create a new account!");
				}
				//sets message if the password is wrong for that username
				else if(res == 0){
					logMessage = ("Wrong password! Please try again!");
				}
				//sets message if login was successful
				else if(res == 1){
					logMessage = ("Congrats! Login successful!");
					Intent i = new Intent();                 
	        		i.setClassName("cs2340.droidceptiontodo", "cs2340.droidceptiontodo.ToDoItemsActivity");  
	        		//Start ToDoItems activity screen
	        		startActivity(i);
				}
			}catch(Exception e){
				logMessage = "An error occurred";
			}
			
			
			/**
			//returns an int representation of the result of verifying user
			int res = Files.verifyUser(getApplicationContext(), lgn, password);
			//sets message if there was an error in reading the file
			if(res == -2){
				logMessage = ("An error has occurred! Please try again!");
			}
			//sets message if a the a file for the user does not exist
			else if(res == -1){
				logMessage = ("User was not found. Click register button to create a new account!");
			}
			//sets message if the password is wrong for that username
			else if(res == 0){
				logMessage = ("Wrong password! Please try again!");
			}
			//sets message if login was successful
			else if(res == 1){
				logMessage = ("Congrats! Login successful!");
				Intent i = new Intent();                 
        		i.setClassName("cs2340.droidceptiontodo", "cs2340.droidceptiontodo.ToDoItemsActivity");  
        		//Start ToDoItems activity screen
        		startActivity(i);
			}
			*/
			//displays (changes the text of a TextView) the result of verifying the user
			TextView tv = (TextView)findViewById(R.id.info); 
			tv.setText(logMessage); //returns a specific logMessage as output
		}
		//Case of Register button  being clicked, switch to Register activity screen
		else {
			if (v.getId() == (register.getId())){
				Intent i = new Intent();                 
        		i.setClassName("cs2340.droidceptiontodo", "cs2340.droidceptiontodo.RegisterActivity");  
        		//Start Register activity screen
        		startActivity(i); 
			}
		}
    }
}